from odoo import fields, models, api


class PurchaseOrder(models.Model):
    _inherit = 'purchase.order'

    state = fields.Selection([
        ('draft', 'RFQ'),
        ('sent', 'RFQ Sent'),
        ('to approve', 'To Approve'),
        ('purchase', 'Purchase Order'),
        ('done', 'Locked'),
        ('cancel', 'Cancelled'),
        ('financial audit','财务审核'),
        ('manager review','经理审核'),
        ('storage','审核完成'),
        ('finish','已拆解')
    ], string='状态', readonly=True, index=True, copy=False, default='draft', track_visibility='onchange')

    @api.multi
    def button_financial_audit(self):
        self.write({'state': 'financial audit'})

    @api.multi
    def button_manager_review(self):
        self.write({'state': 'manager review'})

    @api.multi
    def button_sales_return(self):
        self.write({'state': 'sales return'})

    @api.multi
    def button_storage(self):
        self.write({'state': 'storage'})

    @api.multi
    def bwe_bom(self):
        super(PurchaseOrder, self).bwe_bom()
        print('我是49行')
        self.write({'state':'finish'})
        purchase_env = self.env['purchase.order']
        purline_env = self.env['purchase.order.line']
        purbom_env = self.env['purchase.bom']
        purchase_bwe = purchase_env.search([('name', '=',self.name+'S')])
        purbom_bwe = purbom_env.search([('name', '=',self.name+'S')])
        purbomline = self.env['purchase.bom.line']
        purchase_bwe.write({'state':'to approve'})
        print('--'*10,'我没问题','---'*10)
        purbom_order_line = purbom_bwe.order_line.ids
        for i in purbom_order_line:
            p = purbomline.browse(i)
            val = {
                'name': p.name,
                'product_qty': p.product_qty,
                'date_planned': p.date_planned,
                'product_uom': 1,
                'product_id': p.product_id.id,
                'price_unit': p.price_unit,
                'order_id': purchase_bwe.id
            }
            purline_env.create(val)
            print()


class Contract(models.Model):
    _name = 'bwe.contract'
    _description = '合同管理'

    order_line = fields.One2many('bwe.contract.line', 'contract_id','生产单')
    name = fields.Char('名字')
    party_a = fields.Many2one('res.partner', string='甲方', index=True)
    party_b = fields.Many2one('res.partner', string='乙方', index=True)
    company_ids = fields.Many2one('res.partner', '公司名')
    type = fields.Char('合同类型')
    date_of_signature = fields.Datetime('签署时间',required=True, index=True, copy=False, default=fields.Datetime.now)
    content = fields.Text('内容')
    deadline = fields.Datetime('终止时间',required=True, index=True, copy=False, default=fields.Datetime.now)

    @api.multi
    def build_of_contract(self):
        pass


class ContractLine(models.Model):
    _name = 'bwe.contract.line'
    contract_id = fields.Many2one('bwe.contract', '合同ID', ondelete='restrict')
    product_id = fields.Many2one('product.template', string='Product')
    price = fields.Float(string='价格')





